<?php

class m_website_actions_block_modal_EditBlockLabelAction extends m_backoffice_extends_FormModal
{
	public function execute()
	{
		$id = $this->getRequest()->getParameter("id");

        if(!isset($id) || $id == '') return '';

		list($name, $documentId, $blockId) = explode("_", $id);

        $this->setModalParameters();
        $formValues = $this->getFormValues('form');

        $tempDocService = m_backoffice_services_TempDocService::getInstance();

        if($tempDocService->hasTemporary($documentId))
        {
            $temp = true;
            $page = $tempDocService->getTemporaryDocument($documentId);
        }
        else
        {
            $temp = false;
            $page = $this->getDocumentProvider()->getByUniqueId($documentId);
        }

        $block = $page->getBlockById($blockId);

        $label = isset($formValues['label']) ? $formValues['label'] : $block['blockLabel'];

        if($this->isFormSubmit())
        {
            if($formValues['label'] != '')
            {
                $block['blockLabel'] = $formValues['label'];

                $page->setBlockParamsById($blockId, $block);

                if($temp)
                {
                    $tempDocService->saveTemporaryDocument($page);
                }
                else
                {
                    $page->save();
                }

                $this->closeModalAndRefresh();
            }
            else
            {
                $errors['label'] = $this->getLocale('backoffice.errors.required');
                $this->addToModel('errors', $errors);
            }

        }

        $this->addToModel('blockLabel', $label);
        $this->setView('success');
    }
}